package cam.School.Services;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import cam.School.DataAccessObject.DAOFactory;
import cam.School.DataAccessObject.EmployeeDAO;
import cam.School.DataAccessObject.StudentDAO;
import cam.School.DataTransfertObject.CallResult;
import cam.School.DataTransfertObject.EmployeeDTO;
import cam.School.DataTransfertObject.StudentDTO;

@Path("/studentService")
public class StudentService {
	//ServiceMethods:
//		 -getAllStudent(/*sort ascending or descending or none*/sortMethode)
//		 -getStudentById(studentId)
//		 -getStudentByName(studentName)
//		 -getStudentByClassId(classId)
//		 -getStudentByClassCategory(categoryName) // 6em 5em...
// 		 -getStudentByMatrikulNumber(studentMatricul)
// 		 -getStudentByBirthdayAndName(studentName, studentBirthday)
//       -insertNewStudent(data)
//       -updateStudent(data)
//       -deleteStudent(studentId)
//		 -getStudentWeeklyPlan(classId)
	
	@GET
	@Path("/getAllStudents")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAllStudents() {		
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		StudentDAO studentDAO = javabase.getStudentDAO();
		List<StudentDTO> students =  studentDAO.getAll();
		return Response.status(Response.Status.OK).entity(students).build();		
	}
	
	@GET
	@Path("/getStudentById/{StudentId}")
	@Produces(MediaType.APPLICATION_JSON)
	public StudentDTO getStudentById(@PathParam("StudentId") int studentId ) {		
		return null;		
	}
	
	@GET
	@Path("/getStudentByClassRoomId")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getByClassRoom(@QueryParam("classRoomId") long classRoomId ) {		
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		StudentDAO studentDAO = javabase.getStudentDAO();	
		
		return Response.ok((ArrayList<StudentDTO>) studentDAO.getByClassRoom(classRoomId)).build();
	}
	
	
	@GET
	@Path("/getByStudentClassRoomGroup")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getByClassRoomGroup(@QueryParam("className") String className ) {		
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		StudentDAO studentDAO = javabase.getStudentDAO();
		return Response.ok((ArrayList<StudentDTO>) studentDAO.getByClassRoomGroup(className)).build();		
	}
	
	
	@GET
	@Path("/getStudentByBirthdayAndName?name={studentName}&birthday={studentBirthday}")
	@Produces(MediaType.APPLICATION_JSON)
	public StudentDTO getStudentByBirthdayAndName(@PathParam("studentName") String studentName ) {		
		return null;		
	}
	
	@GET
	@Path("/getStudentByMaticulNumber")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getStudentByMaticulNumber(@QueryParam("studentMatricul") String studentMatricul) {		
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		StudentDAO studentDAO = javabase.getStudentDAO();

		StudentDTO student = studentDAO.getByImmatricutionNumber(studentMatricul);
		
		if(student != null){
			return Response.status(Response.Status.OK).entity(student).build();		
		}
		return Response.status(Response.Status.NOT_FOUND).entity(new CallResult(405,"not found")).build();		
	}	
		
	@POST
	@Path("/createNewStudent")
	@Produces(MediaType.APPLICATION_JSON)
	public Response createNewStudent(StudentDTO student) {				  
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		StudentDAO studentDAO = javabase.getStudentDAO();

		long studentId = studentDAO.create(student).getResultCode();
		
		if((new Integer((int)studentId).equals(new Integer(406))) == false){
			return Response.status(Response.Status.OK).entity(studentId).build();		
		}
		return Response.status(Response.Status.UNAUTHORIZED).entity(studentId).build();		
	}
	
	@PUT
	@Path("/updateStudent")
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateStudent(StudentDTO student ) {				  
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		StudentDAO studentDAO = javabase.getStudentDAO();
		
		CallResult studentCreateResult = studentDAO.update(student);
		long studentId = studentCreateResult.getResultCode();
		
		if((new Integer((int)studentId).equals(new Integer(406))) == false){
			return Response.status(Response.Status.OK).entity(studentId).build();			
		}		
		return Response.status(Response.Status.UNAUTHORIZED).entity(studentCreateResult).build();
			
	}
	
	
	@GET
	@Path("/deleteStudent")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteStudent(@QueryParam("studentId") long studentId) throws Exception {				  
		DAOFactory javabase = DAOFactory.getInstance("javabase.jdbc");
		StudentDAO studentDAO = javabase.getStudentDAO();
		return Response.ok(studentDAO.delete(studentId)).build();			
	}	
	
	
	
	
	@POST
	@Path("/getStudentWeeklyPlan?id={studentMatricul}")
	@Produces(MediaType.TEXT_XML)
	public String getStudentWeeklyPlan(@PathParam("studentMatricul") String studentMatricul ) {				  
		//TODO 
		return "TO DO"; 		
	}

}
